import { request } from "vendor/utils/T";
import { toApi } from '@/service/proxyApi';
import { permission } from "@/service/permission";
import { RespDataType, RequestDataType } from "./apiType";

export * from "./apiType";
const { postJSON, get, removeToken } = request;

/**
 * 获取验证码
 */
export const getCaptchaImage = () => get<RespDataType.ICaptchaImageInfo>(toApi("api/v1/pub/captcha/get"));

/**
 * 登录
 */
export const login = (params: RequestDataType.LoginParams) => postJSON(toApi("api/v1/system/login"), params);   // 新版

/**
 * 退出
 */
export const logout = () => new Promise((resolve, reject) => {
  get(toApi("api/v1/system/logout")).then(() => {
    permission.loginUser.clear()
    removeToken();
    resolve(true)
  }).catch((e) => reject(e))
});

/**
 * 检查是否登录
 */
export const checkLogin = (isShowErrorPrompt = true) => new Promise((resolve, reject) => {
  get(toApi("checkLogin"), {}, { isShowErrorPrompt }).then(() => {
    resolve(true)
  }).catch((e) => {
    permission.loginUser.clear()
    reject(e)
  })
});

/**
 * 获取用户信息
 */
export const getUserInfo = () => get<RespDataType.UserInfo>(toApi("api/v1/system/user/info")).then((resp) => {
  permission.loginUser.set(resp.data)
}).catch((e) => {
  permission.loginUser.clear();
  console.error(e);
});
